﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using IanWebkitBrowser;

namespace Process
{
    public partial class Frame : Form
    {
        public IanBrowser wbView;

        private XmlDocument config = new XmlDocument();

        private static bool isCefReleased = false;

        private int GetBrowserWidth()
        {
            return wbView.ScrollBarsEnabled ? Width - 18 : Width;
        }

        private int GetBrowserHeight()
        {
            return wbView.ScrollBarsEnabled ? Height - 40 : Height;
        }

        private void SetUI()
        {
            config = new XmlDocument();
            config.Load("config/Process.xml");
            Width = int.Parse(config["Config"]["UIWidth"].InnerText);
            Height = int.Parse(config["Config"]["UIHeight"].InnerText);
            Text = config["Config"]["UITitle"].InnerText;
            Icon = new Icon(config["Config"]["UIIcon"].InnerText);
            bool hasWrapper = bool.Parse(config["Config"]["UIWrapper"].InnerText);
            
            if (hasWrapper)
            {
                FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

        public Frame()
        {
            InitializeComponent();
            SetUI();
            LoadBrowser();
        }

        private void LoadBrowser()
        {
            wbView = new IanBrowser();
            wbView.ScrollBarsEnabled = bool.Parse(config["Config"]["UIScroll"].InnerText);
            wbView.HomeUrl = "http://www.baidu.com";
            wbView.Width = GetBrowserWidth();
            wbView.Height = GetBrowserHeight();
            wbView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            wbView.Visible = true;
            Controls.Add(wbView);
            wbView.Init(new string[] { });
        }

        private void wbView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            //if (ConstantPool.Instance.Get<bool>("Debug_EscExit") == true)
            //{
            //    if (e.KeyCode == Keys.Escape)
            //    {
            //        Application.Exit();
            //    }
            //}
        }

        private void fmFrame_Load(object sender, EventArgs e)
        {
            try
            {
                //SetUI();
                //Util.Instance.StartSystem();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        protected override void OnShown(EventArgs e)
        {
            try
            {
                base.OnShown(e);
                new System.Threading.Thread(x =>
                {
                    while (true)
                    {
                        if (IanBrowser.App.Browser != null)
                        {
                            ProcessEngine.Instance.SetProcess();
                            break;
                        }
                        System.Threading.Thread.Sleep(200);
                    }
                    OnResize(null);
                }).Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Frame_FormClosing(object sender, FormClosingEventArgs e)
        {
            ProcessEngine.Instance.Exit();
            if (!isCefReleased)
            {
                wbView.Release();
                isCefReleased = true;
            }
        }
    }
}
